package 蓝桥杯算法;

public class day18 {
    public static void main(String[] args) {
        day18 a=new day18();
        int[] arr={1,7,4,9,2,5};
        int[] arr2={-2,1,-3,4,-1,2,1,-5,4};
        int[] arr3={7,1,5,3,6,4};
       /* System.out.println(a.wigleMaclength(arr));*/
        System.out.println(a.MaxSubArray(arr2));
        System.out.println(a.maxporfit(arr3));

    }

   public int wigleMaclength(int[] arr) {
       if (arr.length <= 1) {
           return arr.length;
       }
       int count = 1;
       int curDiff = 0;//当前差值
       int preDiff = 0;//上一个差值
       for (int i = 1; i < arr.length; i++) {
           curDiff = arr[i] - arr[i - 1];
           if ((curDiff > 0 && preDiff <= 0) || (curDiff < 0 && preDiff >=0)) {
              count++;
              preDiff=curDiff;
           }
       }
       return count;
   }
   public int maxcount(int[] arr){
        int Maxsum=0;
        for(int i=0;i<arr.length;i++){
            int count=0;//每次都要定义一次新的count
            for(int j=i;j<arr.length;j++){
                count=count+arr[j];
                if(count>Maxsum){
                    Maxsum=count;
                }
            }
        }
        return Maxsum;

   }
   public int MaxSubArray(int[] num){//贪心算法
        if(num.length==1){
            return num[0];
        }
        int sum=0;
        int count=0;
        for(int i=0;i<num.length;i++){
            count=count+num[i];
            sum=Math.max(sum,count);
            if(count<=0){
                count=0;//减少值的消耗
            }
        }
        return sum;
   }
   public int maxporfit(int[] num){
        int result=0;
        for(int i=1;i<num.length;i++){
            result+=Math.max(num[i]-num[i-1],0);
        }
        return result;
   }
   public boolean canJump(int[] nums){
        if(nums.length==0){
            return true;
        }
        int cover=0;
        for(int i=0;i<nums.length;i++){
            cover=Math.max(cover,nums[i]+1);
            if(cover>nums.length-1);
            return true;
        }
        return false;
   }
}
